package com.datastructure.array;

import javax.sound.midi.MidiChannel;
import javax.swing.border.Border;
import java.awt.image.RGBImageFilter;

/**
 * @author: 临晖
 * @date: 2023/10/06
 * @description:
 */
public class Utils {


    /**
     * 二分查找法寻找右边界
     * @param nums
     * @param target
     * @return
     */
    public static int getRightBorder(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int rightBorder = -2;

        while (left <= right) {
            int middle = left + ((right - left) >> 1);      //如果是左开则需要加1

            if (target < nums[middle]) {
                right =  middle - 1;
            } else {
                left = middle + 1;
                rightBorder = left;
            }
        }

        return rightBorder;
    }


    /**
     * 寻找左边界
     * @param nums
     * @param target
     * @return
     */
    public static int getLeftBorder(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int leftBorder = -2;

        while (left <= right) {
            int middle = left + ((right - left) >> 1);      //如果是左开则需要加1

            if (target > nums[middle]) {
                left = middle + 1;
            } else {
                right = middle - 1;
                leftBorder = right;
            }
        }

        return leftBorder;

    }
}
